package com.neusoft.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration

public class MybatisConfig {

    @Bean/*将返回值交给IOC容器 需要的时候会自动注入 */
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DruidDataSource druidDataSource){
        //生成数据库连接对象
        SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();
        ssfb.setDataSource(druidDataSource);
        ssfb.setTypeAliasesPackage("com.neusoft.domain");
        //设置分页插件及其属性
       Properties properties = new Properties();
        properties.setProperty("helper-dialect","mysql");
        properties.setProperty("reasonable","true");
        PageInterceptor pi = new PageInterceptor();
        pi.setProperties(properties);
        ssfb.setPlugins(new PageInterceptor[]{pi});
        return ssfb;
    }
    @Bean
    public MapperScannerConfigurer getMapperScannerConfigurer(){
        //扫描mapper接口 生成代理对象
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setBasePackage("com.neusoft.mapper");
        return msc;
    }
}
